package com.hfzy.ihk.web.freeswitchManage.job;

import com.hfzy.ihk.facade.freeswitchManage.entity.Gateway;
import com.hfzy.ihk.web.freeswitchManage.biz.PushToWebsocketBiz;
import com.hfzy.ihk.web.freeswitchManage.constant.RedisKeys;
import com.hfzy.ihk.web.freeswitchManage.mapper.GatewayCardSlotMapper;
import com.hfzy.ihk.web.freeswitchManage.mapper.GatewayMapper;
import com.hfzy.ihk.web.freeswitchManage.support.swx.SwxEquipmentControlProcess;
import com.hfzy.ihk.web.freeswitchManage.vo.GatewayCardSlotVo;
import com.hfzy.ihk.web.freeswitchManage.vo.RedisData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.Profile;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * Created by YANFA on 2019/4/10.
 */
@Slf4j
@Component
@Profile({"pro","dev"})
public class CheckSwxChannelStateJob {

    @Autowired
    PushToWebsocketBiz pushToWebsocketBiz;

    @Autowired
    GatewayMapper gatewayMapper;

    @Autowired
    GatewayCardSlotMapper gatewayCardSlotMapper;

    @Autowired
    SwxEquipmentControlProcess swxEquipmentControlProcess;
    /**
     * 每三分钟随机校验5个中继设备信息
     * 查询中继中当前的每个通道的状态，idl ,inuse,...
     */
    @Scheduled(cron = "0 0/3 * * * ?")
    public void sendMsgForCheckChannelState(){

        //获取所有中继名，然后发送所有中继状态
        log.info(">>>>开始执行校验 queuejob");
        List<Gateway> gatewayList = gatewayMapper.queryRandTwoLimit(5);
        for(Gateway gateway:gatewayList){
            pushToWebsocketBiz.checkChannelTrunk(gateway.getNum());
        }
        log.info(">>>>执行校验 queuejob 结束");

    }


    /**
     * 定时跑job检查redis通道 信息map的数据准确情况key:sn:swx1
     * 查询数据库中通道信息更新到redis 用于实时同步社保的sipuser，当前通道号码等信息等信息
     */
    @Scheduled(cron = "0 0/3 * * * ?")
    public void checkChannelMap(){
        log.info(">>>>开始执行校验map job");
        List<GatewayCardSlotVo> gatewayCardSlotVos = gatewayCardSlotMapper.queryAllActiveChannelTrunkInfo();
        for(GatewayCardSlotVo gatewayCardSlotVo:gatewayCardSlotVos){
            try {
                RedisData redisData = new RedisData();
                redisData.setSn(gatewayCardSlotVo.getNum());
                redisData.setTrunkName(RedisKeys.SWX + gatewayCardSlotVo.getChannel());
                swxEquipmentControlProcess.setChannelSlotStateMap(redisData, gatewayCardSlotVo);
            }catch (Exception e){
                log.error(">>定时检查通道信息 job 异常",e);
            }
        }
        log.info(">>>>执行校验map job 结束");

    }

}
